﻿using System;
using System.Collections.Generic;

using DeviceManager.Device;
using DeviceManager.Common;
using LoggingClient;

using LittleATM.Interop.KXBape;

namespace DeviceManager.Kal.KXDevice
{
    public class KXPinPadImpl : KXDeviceImpl, IPinPad
    {
        #region Common Override Code
        KXBape pinPad = null;

        public override bool Init()
        {
            try
            {
                pinPad = new KXBape();
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex.ToString());

                return false;
            }

            return true;
        }

        public override bool OpenConnection()
        {
            short ret = pinPad.OpenConnection();

            DMLogger.Instance.Info(string.Format("KXPinPadImpl.OpenConnection() = {0}", ret));

            return true;
        }

        public override bool CloseConnection()
        {
            short ret = pinPad.CloseConnection();

            DMLogger.Instance.Info(string.Format("KXPinPadImpl.CloseConnection() = {0}", ret));

            return true;
        }

        public override bool SubscribeEvents()
        {
            try
            {
                pinPad.ConnectionOpened += OnConnectionOpened;
                pinPad.ConnectionClosed += OnConnectionClosed;
                pinPad.DeviceError += OnDeviceError;
                pinPad.FatalError += OnDeviceError;
                pinPad.StatusChanged += OnStatusChanged;
                pinPad.Timeout += OnTimeout;
                pinPad.Locked += OnLocked;
                pinPad.LockTimeout += OnLockTimeout;
                pinPad.Unlocked += OnUnlocked;
                pinPad.EnabledEventsDeferred += OnEnabledEventsDeferred;

                return true;
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex);
                return false;
            }
        }

        public override bool UnSubscribleEvents()
        {
            try
            {
                pinPad.ConnectionOpened -= OnConnectionOpened;
                pinPad.ConnectionClosed -= OnConnectionClosed;
                pinPad.DeviceError -= OnDeviceError;
                pinPad.FatalError -= OnDeviceError;
                pinPad.StatusChanged -= OnStatusChanged;
                pinPad.Timeout -= OnTimeout;
                pinPad.Locked -= OnLocked;
                pinPad.LockTimeout -= OnLockTimeout;
                pinPad.Unlocked -= OnUnlocked;
                pinPad.EnabledEventsDeferred -= OnEnabledEventsDeferred;



                return true;
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex);
                return false;
            }
        }
        #endregion

        #region IPinPad Interface Impl
        public bool UserEntry(short MaxKeys, bool AutoEnd, string ActiveKeys, string TerminatorKeys, int Timeout)
        {
            int retRes = pinPad.UserEntry(MaxKeys, AutoEnd, ActiveKeys, TerminatorKeys, Timeout);

            DMLogger.Instance.Info("UserEntry: " + retRes);

            return true;
        }

        public bool GetVariablePIN(short MinLength, short MaxLength, string Format, string CustomerData, short PadChar,
            string KeyName, string XORData, string SecondKeyName, int Timeout)
        {
            int retRes = pinPad.GetVariablePIN(MinLength, MaxLength, Format, CustomerData, PadChar,
             KeyName,  XORData,  SecondKeyName,  Timeout);

            DMLogger.Instance.Info("GetVariablePIN: " + retRes);

            return true;
        }

        public bool CancelUserEntry()
        {
            int retRes = pinPad.CancelUserEntry();
            DMLogger.Instance.Info("CancelUserEntry: " + retRes);

            return true;
        }

        public bool CancelPINEntry()
        {
            int retRes = pinPad.CancelPINEntry();
            DMLogger.Instance.Info("CancelPINEntry: " + retRes);

            return true;
        }

        public bool IsValidKeySet(string keys)
        {
            bool retRes = pinPad.IsValidKeySetSync(keys);

            DMLogger.Instance.Info(string.Format("IsValidKeySet[{0}]: {1}", keys, retRes));

            return retRes;
        }
        #endregion

    }
}
